<?php
include_once('./config/config.php');
include_once('./config/check.php');

// 获取当前的页码
$page = isset($_GET['page']) ? $_GET['page'] : 1;

// 获取pre_jobs表的数据总数
$CountSql = "SELECT COUNT(*) as c FROM `pre_jobs`";
$count = find($CountSql);

// 每页显示多少条数据
$limit = 5;

// 每页显示多少个分页的页码数
$size = 10;

//调用分页函数
$PageStr = page($page, $count['c'], $limit, $size, "badoo");

// 偏移量 -> 索引值
$start = ($page - 1) * $limit;

// 按索引值以及每页显示多少条数据去查询admin表的数据
$jobsql = "SELECT * FROM `pre_jobs` ORDER BY id ASC LIMIT $start,$limit";
// 获取当前页的数据
$AdminList = all($jobsql);
// var_dump($AdminList);


if($_POST){
    $id = empty($_POST['id']) ? 0 : trim($_POST['id']);

    $sql = "SELECT * FROM {$pre_}jobs WHERE `id`= $id ";
    $data = find($sql);
    
    $data = array_filter($data);  //array_filter() 函数用回调函数过滤数组中的值
    $afffect = del('jobs', 'id=' . $id);//执行删除

    if($afffect){
        ShowMsg('删除成功', 'job.php');
        exit;
    }else{
        ShowMsg('删除失败', 'job.php');
        exit;
    }

}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <!-- 分页样式 -->
    <link rel="stylesheet" href="./assets/page/css.css">
    <?php include_once('common/meta.php'); ?>
</head>


<body>
    <!--<![endif]-->

    <?php include_once('common/header.php'); ?>

    <?php include_once('common/menu.php'); ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">职位列表</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.php">Home</a> <span class="divider">/</span></li>
            <li class="active">Index</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='jobAdd.php'"><i class="icon-plus"></i>职位管理</button>
                </div>
                <div class="well">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>职位</th>
                                <th style="width: 50px;">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($AdminList as $item) { ?>
                                <tr>
                                    <td><?php echo $item['id']; ?></td>
                                    <td><?php echo $item['name']; ?></td>

                                    <td>
                                        <a href="jobEdit.php?id=<?php echo $item['id']; ?>"><i class="icon-pencil"></i></a>
                                        <a onclick="del(<?php echo $item['id']; ?>)" href="#myModal" role="button" data-toggle="modal"><i class="icon-remove"></i></a>
                                    </td>
                                </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>
                <div class="pagination">
                    <?php echo $PageStr; ?>
                </div>
                <!-- 弹出框 -->
                <form method="post">
                    <!-- 隐藏域 装的value的是 我们要删除的ID -->
                    <input type="hidden" name="id" value="" />

                    <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            <h3 id="myModalLabel">确认框</h3>
                        </div>
                        <div class="modal-body">
                            <p class="error-text">
                                <i class="icon-warning-sign modal-icon"></i>
                                是否确认删除该职位？
                            </p>
                        </div>
                        <div class="modal-footer">
                            <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
                            <button class="btn btn-danger" type="submit">确认</button>
                        </div>
                    </div>
                </form>
                <?php include_once('common/footer.php'); ?>

            </div>
        </div>
    </div>
</body>

</html>
<?php include_once('common/script.php'); ?>
<script>
    //单条删除
    function del(id) {
        //将点击删除的ID 赋值给 隐藏域，让表单进行提交  提交给php 让php拿到id 在进行删除
        $("input[name='id']").val(id) //val() 方法返回或设置被选元素的值。
    }
</script>